<?php
declare (strict_types=1);

namespace app\service;

use app\common\model\Code;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/7
 * Time: 11:51
 * qq 571031767
 */

class EmailService
{
    //邮件发送 - 验证码类型
    public static function send($toUser=['571031767@qq.com'])
    {
        // 创建Transport对象，设置邮件服务器和端口号，并设置用户名和密码以供验证
        $transport = (new \Swift_SmtpTransport(config('email.smtp_address'), config('email.smtp_port'),config('email.encryption')))
            ->setUsername(config('email.username'))
            ->setPassword(config('email.password'));
        // 创建mailer对象
        $mailer = new \Swift_Mailer($transport);
        $code = rand(100000,999999);//生成一个数字 6位的
        // Create a message
        $message = (new \Swift_Message('网站验证码：'))
            ->setFrom([config('email.username') => config('email.name')])
            ->setTo($toUser)
            ->setBody("米醋儿网验证码：{$code},若不是本人操作请忽略本邮件。");
        // Send the message
        $result = $mailer->send($message);
        if($result){
            $data["name"] = $toUser[1];
            $data["code"] = $code;
            $data["type"] = 1;//1邮箱   2电话
            Code::create($data);//保存数据库
        }
        return $result;
    }

    //邮件发送 - 报告
    public static function ropot($toUser=['571031767@qq.com'],$msg="米醋儿网内容。")
    {
        // 创建Transport对象，设置邮件服务器和端口号，并设置用户名和密码以供验证
        $transport = (new \Swift_SmtpTransport(config('email.smtp_address'), config('email.smtp_port'),config('email.encryption')))
            ->setUsername(config('email.username'))
            ->setPassword(config('email.password'));
        // 创建mailer对象
        $mailer = new \Swift_Mailer($transport);
        $code = rand(100000,999999);//生成一个数字 6位的
        // Create a message
        $message = (new \Swift_Message('米醋儿网每日报告 - micuer：'))
            ->setFrom([config('email.username') => config('email.name')])
            ->setTo($toUser)
            ->setBody($msg);
        // Send the message
        $result = $mailer->send($message);
        if($result){
            $data["name"] = $toUser[1];
            $data["code"] = $code;
            $data["type"] = 1;//1邮箱   2电话
            Code::create($data);//保存数据库
        }
        return $result;
    }
}


